package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.AppointmentsDao;
import dao.impl.AppointmentsDaoImpl;
import entity.Appointments;
import entity.vo.DoctorsGetAppointments;
import utils.GetFieldUtils;
import utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DoctorsAppointmentsServlet", urlPatterns = "/bigProjects/DoctorsAppointmentsServlet")

public class DoctorsAppointmentsServlet extends HttpServlet {

    //获取这个医生收到的预约
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AppointmentsDao appointmentsDao = new AppointmentsDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer doctorsId = Integer.valueOf(req.getParameter("doctorsId"));

        List<DoctorsGetAppointments> doctorsGetAppointments = appointmentsDao.queryByDoctorsId(doctorsId);
        resp.getWriter().write(ResponseResult.success(doctorsGetAppointments).toString());
    }

    //确认预约收到;需要一个int类型的id
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AppointmentsDao appointmentsDao = new AppointmentsDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String str = GetFieldUtils.getRequestPayload(req);
        ObjectMapper mapper = new ObjectMapper();
        Appointments appointments = mapper.readValue(str, Appointments.class);

        int b = appointmentsDao.confirmById(appointments.getId());
        if (b > 1) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("确认失败").toString());
    }

    //查询已完成的挂号
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AppointmentsDao appointmentsDao = new AppointmentsDaoImpl();
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer doctorsId = Integer.valueOf(req.getParameter("doctorsId"));

        List<DoctorsGetAppointments> doctorsGetAppointments = appointmentsDao.queryOkByDoctorsId(doctorsId);
        resp.getWriter().write(ResponseResult.success(doctorsGetAppointments).toString());
    }
}
